// pages/home/home.js
const http = require('../../ajax.js')
const api = require('../../api.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    xiabiao: [],
    ida: [],
    splb: [],
    city: [],
    spfla: [],
    index: [],
    idaa: []
  },
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  toalnaddress() {
    wx.navigateTo({
      url: '/pages/LNE_address/LNE_address',
    })
  },
  tuangou() {
    wx.navigateTo({
      url: 'tuangou/tuangou',
    })
  },
  ditu() {
    wx.navigateTo({
      url: 'ditu/ditu',
    })
  },
  sousuo() {
    wx.navigateTo({
      url: 'sousuo/sousuo',

    })
  },
  xiang(e) {

    console.log(e)
    let num = e.currentTarget.dataset.index;
    console.log(num)
    wx.navigateTo({
      url: 'xiang/xiang?id=' + num,
    })
  },
  //分类接口
  bksp() {
    http(api.spfl, "params",
      {
        shopId: 18,
        storeId: 56200
      },
      "POST"
    ).then(res => {
      console.log("1111", res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          spfla: res.data.result,
          ida: res.data.result[0].categoryId
        })
        this.getGoodsList()
      }
      // console.log("aaa",this.data.spfla)
    })
  },
  //获取分类商品列表
  getGoodsList() {
    let date = {
      "status": "1",
      "shopId": 18,
      "storeId": 56200,
      "salesCategoryId": this.data.ida,
      "sortType": 5,
      "isAsc": false,
      "teminal": 2,
      "customerId": 589,
      "currentPage": 0,
      "pageSize": 15
    }
    http(api.splb, "data", date, "POST").then(res => {
      //console.log(res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          splb: res.data.result.items
        })
      }
      console.log("aaa111", this.data.splb)
      wx.setStorageSync("spxq", this.data.splb)
    })
  },
  clickTab2(e) {
    this.setData({
      xiabiao: e.target.dataset.categoryid,
      ida: e.currentTarget.dataset.id.categoryId,
    })
    this.getGoodsList()
    //console.log(e)
    //console.log(this.data.xiabiao)
    //console.log(this.data.ida)
  },
  jia(e) {
    var that = this
    //console.log(e)
    this.setData({
      idaa: e.currentTarget.dataset.id
    })
    //console.log(this.data.idaa)
    let data = {
      customerId: 589,
      shopId: 18,
      goodsId: this.data.idaa,
      quantity: 598,
      teminal: 2,
      storeId: 56200
    }
    http(api.slja, "data", data, "POST").then(res => {
      console.log('2222222222222222222222', res)
      this.getGoodsList()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadInfo();
  },
  loadInfo: function () {
    var page = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var longitude = res.longitude
        var latitude = res.latitude
        page.loadCity(longitude, latitude)
      },
      fail: function () {
        // fail  
      },
      complete: function () {
        // complete  
      }
    })
  },
  loadCity: function (longitude, latitude) {
    var page = this
    wx.request({
      url: 'http://api.map.baidu.com/geocoder/v2/?ak=HbUVYMUg6PwbOnXkztdgSQlQ&location=' + latitude + ',' + longitude + '&output=json&pois=1',
      //这里的ak 是去百度地图api获取的需要自己登陆获取一下  地址：https://lbsyun.baidu.com/index.php?title=wxjsapi
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // success  
        // console.log(res);
        var city = res.data.result.formatted_address;


        console.log("城市为" + city)
        page.setData({ city: city });
      },
      fail: function () {
        // fail  
      },
      complete: function () {
        // complete  
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.bksp()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})